using System;
using System.Linq;
using GameStateManagement.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;

namespace GameStateManagement.View
{
    class CameraKeyBoardInputHandler : KeyBoardInputHandler
    {
        private readonly Camera _camera;

        public CameraKeyBoardInputHandler(InputFsm machine, Camera camera)
            : base(machine)
        {
            _camera = camera;
        }

        private void HandleZoom(bool zoomIn, Vector2 position)
        {
            //var pinchCenter = new Vector3((gesture.Position + gesture.Position2) / 2, 0);

            //var oldDistance = Vector2.Distance(gesture.Position - gesture.Delta,
            //                                   gesture.Position2 - gesture.Delta2);
            //var newDistance = Vector2.Distance(gesture.Position, gesture.Position2);

            //var scale = newDistance / oldDistance;

            var scale = zoomIn ? 1 : -1;

            _camera.Zoom(new Vector3(position, 0), scale);
        }

        public override bool HandleInput(InputState state)
        {
            bool zoom = false, dosomething = false, inputHandled = false;
            if(state.CurrentKeyboardStates[0].IsKeyDown(Keys.A))
            {
                zoom = true;
                dosomething = true;
            }
            if(state.CurrentKeyboardStates[0].IsKeyDown(Keys.A))
            {
                zoom = false;
                dosomething = true;
            }

            if(dosomething)
            {
                var gesture = (from gSample in state.Gestures
                              where gSample.GestureType == GestureType.FreeDrag
                              select gSample).SingleOrDefault();

                HandleZoom(zoom, gesture.Position);
                inputHandled = true;
            }

            return inputHandled;
        }

        #region Overrides of KeyBoardControler

        public override void HandleA()
        {

        }

        public override void HandleB()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
